﻿using MySql.Data.MySqlClient;
using NB.MGJ.FrameWork;
using NB.MGJ.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Service.Origins
{
    public partial class OriginsNodeService
    {
        private BaseRepository _baseRepository = new BaseRepository();

        #region  增
        /// <summary>
        /// 添加节点 产品流程  NodeType = 1 模板流程  NodeType = 0
        /// </summary>
        /// <returns></returns>
        public int Add(Origins_Node model)
        {
            int result = _baseRepository.InsertReturnID<Origins_Node>(model);
            return result;
        }



        #endregion

        #region 删
        public int Del(int id)
        {
            string sql = "update Origins_Node set ViewDel=1 where ID=" + id;
            int result = _baseRepository.ExecuteSql<int>(sql);
            return result;
        }


        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="FlowID"></param>
        /// <param name="NodeType">1 产品流程 0 模板</param>
        /// <returns></returns>
        public int Del_FlowID(int FlowID, int NodeType)
        {
            string sql = "delete from Origins_Node  where FlowID=" + FlowID + " and NodeType=" + NodeType;
            int result = _baseRepository.ExecuteSql<int>(sql);
            return result;
        }
        #endregion

        #region 改
        public int Update(Origins_Node model)
        {
            int result = _baseRepository.Update<Origins_Node>(model);
            return result;
        }
        #endregion

        #region 查
        public Origins_Node GetModel(int id)
        {
            string sql = "select * from Origins_Node where ID=" + id;
            var model = _baseRepository.ExecuteSqlCollection<Origins_Node>(sql).ToList().FirstOrDefault();
            return model;
        }



        public PagedList<Origins_Node> GetPageList(ref PagerModel pager)
        {

            try
            {
                StringBuilder strSql1 = new StringBuilder();
                strSql1.Append(@" Origins_Node ");
                var strSql = new StringBuilder(strSql1.ToString().Trim(','));
                pager.FldName = " * ";
                pager.TblName = strSql.ToString();

                if (string.IsNullOrEmpty(pager.StrCondition) || pager.StrCondition == " ")
                {
                    pager.StrCondition = " 1=1 ";
                }
                if (string.IsNullOrEmpty(pager.FldSort) || pager.FldSort == "")
                    pager.FldSort = "  ChangeDate desc ";

                MySqlParameter[] parameters = _baseRepository.SqParameterRefactor(ref pager);


                IEnumerable<Origins_Node> resultList = _baseRepository.ExecuteStoredProcedureCollection<Origins_Node>("Common_PageList", parameters);
                return new PagedList<Origins_Node>(resultList.ToList(), pager.Page, pager.PageSize, 1, Convert.ToInt32(parameters[6].Value));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }






        #endregion

        #region  其他
        public List<Origins_Node> GetNodeAllByFlowId(int? flowId, int type = 0)
        {
            string sql = "select * from Origins_Node where FlowID=" + flowId + " and ViewDel=0 and NodeType=" + type + " ORDER BY NodeName desc,ChangeDate asc ";
            var result = _baseRepository.ExecuteSqlCollection<Origins_Node>(sql).ToList();
            return result;
        }

        public List<Origins_Node> GetNodeAllByFlowId(int? flowId)
        {
            string sql = "select * from Origins_Node where FlowID=" + flowId + " and ViewDel=0 ORDER BY NodeName desc,ChangeDate asc ";
            var result = _baseRepository.ExecuteSqlCollection<Origins_Node>(sql).ToList();
            return result;
        }
        public List<Origins_Node> GetNodeAll(int flowId, int batchId)
        {

            string sql = "select * from Origins_Node where  ViewDel=0 and  ((FlowID =" + flowId + " and NodeType=0) or (FlowID =" + batchId + " and NodeType=1)) ";
            var result = _baseRepository.ExecuteSqlCollection<Origins_Node>(sql).ToList();
            return result;
        }
        #endregion




    }
}
